package cn.bid.consumer.service;

import cn.bid.consumer.client.UserFineClient;
import cn.bx.pojo.AjaxResult;
import cn.bx.pojo.Page;
import cn.bx.pojo.User;
import cn.bx.pojo.Userrole;
import cn.bx.service.UserServiceI;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;

@Service
public class UserService implements UserServiceI {

//    @Autowired
//    RestTemplate restTemplate;
//  @Autowired
//    DiscoveryClient discoveryClient;
    @Autowired
    UserFineClient userFineClient;


    public AjaxResult get(long id){
//   List<ServiceInstance> bids= discoveryClient.getInstances("bid-test");
//        ServiceInstance bid=bids.get(0);
//       String url="http://"+bid.getHost()+":"+bid.getPort()+"/userGet?id="+id;
//        System.out.println("url>>>>>>>>>>>>>>"+url);
  //      String url="http://localhost:8888/userGet?id="+id;
    //    return restTemplate.getForObject(url,AjaxResult.class);
        return  userFineClient.userGet(id);
    }

    @Override
    public AjaxResult login(String username, String pass) {
//        String url="http://localhost:8888/login1?username="+username+"&password="+pass;
//
//        return restTemplate.getForObject(url,AjaxResult.class);
        return  userFineClient.login(username,pass);
    }





    @Override
    public Page<User> search(int pageNo, int limit, String name, Date start, Date end) {

/*        String url="http://localhost:8888/userSearch?page="+pageNo+"&limit="+limit+"&name="+name+"&startDate="+start+"&endDate="+end;
        return restTemplate.getForObject(url,Page.class);*/
       return  userFineClient.search(pageNo, limit, name, start, end);
    }

    @Override
    public AjaxResult userAdd(User user) {
      return  userFineClient.userAdd(user);
    }

    @Override
    public User userGet(long id) {
        return null;
    }

    @Override
    public AjaxResult userUpdate(User user) {
        return userFineClient.userUpdate(user);
    }

    @Override
    public AjaxResult del1(long id) {
        return userFineClient.del1(id);
    }

    @Override
    public AjaxResult batchDel(long[] ids) {
     return  userFineClient.batchDel(ids);
    }

    @Override
    public AjaxResult checkUserNameRepeat(String userName, Long id) {
        return userFineClient.checkUserNameRepeat(userName,id);
    }

    @Override
    public AjaxResult error(Exception e) {
        return null;
    }

    public  List<Userrole> findAll(){
        return  userFineClient.findALL();
    }

}
